package com.auto.survey.activity.monitoring.entity

import android.os.Parcel
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey

@Entity(
    tableName = "Point",
    foreignKeys = [
        ForeignKey(
            entity = MonitoringObject::class,
            parentColumns = ["id"],
            childColumns = ["monitoringObjectId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["monitoringObjectId"])]
)
data class Point(
    /**
     * 测点ID，主键，自增长
     */
    @PrimaryKey(autoGenerate = true) var id: Int = 0,
    /**
     * 所属监测对象ID，外键关联ATS_Monitoring表
     */
    val monitoringObjectId: Int,
    /**
     * 测点名称
     */
    val pointName: String,
    /**
     * 测点X坐标
     */
    var pointX: Double,
    /**
     * 测点Y坐标
     */
    var pointY: Double,
    /**
     * 测点Z坐标（高程）
     */
    var pointZ: Double,
    /**
     * 测点类型（控制点、测点）
     */
    val pointType: String
): Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readInt(),
        parcel.readInt(),
        parcel.readString() ?: "",
        parcel.readDouble(),
        parcel.readDouble(),
        parcel.readDouble(),
        parcel.readString() ?: ""
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeInt(id)
        parcel.writeInt(monitoringObjectId)
        parcel.writeString(pointName)
        parcel.writeDouble(pointX)
        parcel.writeDouble(pointY)
        parcel.writeDouble(pointZ)
        parcel.writeString(pointType)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Point> {
        override fun createFromParcel(parcel: Parcel): Point {
            return Point(parcel)
        }

        override fun newArray(size: Int): Array<Point?> {
            return arrayOfNulls(size)
        }
    }

}